package ml.coursedesign.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.controller.SubjectController;
import ml.coursedesign.entity.*;
import ml.coursedesign.mapper.SubjectGroupMapper;
import ml.coursedesign.service.SelectSubjectService;
import ml.coursedesign.service.StudentService;
import ml.coursedesign.service.SubjectGroupService;
import ml.coursedesign.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SubjectGroupServiceImpl extends ServiceImpl<SubjectGroupMapper,SubjectGroup> implements SubjectGroupService {

    @Autowired
    SubjectService subjectService;
    @Autowired
    SelectSubjectService selectSubjectService;
    @Autowired
    StudentService studentService;

    @Override
    public List <GroupVo> selectGroupPage(String current, String limit, SubjectGroup subjectGroup) {



            QueryWrapper <SubjectGroup> queryWrapper = new QueryWrapper<>();
            if(subjectGroup.getGroupLeader()!= null && "".equals(subjectGroup.getGroupLeader().trim()) ){
                queryWrapper.eq("group_leader",subjectGroup.getGroupLeader()) ;

            }
            if(subjectGroup.getGroupId()!= null &&!"".equals(subjectGroup.getGroupId().trim())){
                queryWrapper.eq("group_id",subjectGroup.getGroupId()) ;

            }
            if(subjectGroup.getCourseClassId()!= null &&!"".equals(subjectGroup.getCourseClassId().trim())){
                queryWrapper.eq("course_class_id",subjectGroup.getCourseClassId()) ;
            }
            if(subjectGroup.getSubjectId()!= null &&!"".equals(subjectGroup.getSubjectId().trim())){
                queryWrapper.eq("subject_id",subjectGroup.getSubjectId()) ;
            }

            Page<SubjectGroup> page = new Page<>(Integer.parseInt(current),Integer.parseInt(limit));
            IPage<SubjectGroup> iPage = baseMapper.selectPage(page, queryWrapper);
            List<SubjectGroup> records = iPage.getRecords();

                List<GroupVo> groupVos = new ArrayList<>();
            for ( SubjectGroup subjectGroup1: records) {

                   GroupVo groupVo = new GroupVo();
                   groupVo.setGroupId(subjectGroup1.getGroupId());
                   int  count = getGroupNum(subjectGroup1.getSubjectId(),subjectGroup1.getCourseClassId());
                   groupVo.setCount(String.valueOf(count));
                   groupVo.setGroupLeaderName(getGroupLeaderName(subjectGroup1.getGroupLeader()));
                   groupVo.setSubjectName(getSubjectName(subjectGroup1.getSubjectId()));
                   groupVo.setSubjectId(subjectGroup1.getSubjectId());
                   groupVo.setCourseClassId(subjectGroup1.getCourseClassId());

                   groupVos.add(groupVo);

            }
        System.err.println(groupVos);
                return groupVos;
    }

    @Override
    public GroupVo getSubjectGroupnfo(String subjectId, String classId) {
        QueryWrapper<SubjectGroup> queryWrapper = new QueryWrapper();

        queryWrapper.eq("subject_id",subjectId);
        queryWrapper.eq("course_class_id",classId);
        SubjectGroup subjectGroup = baseMapper.selectOne(queryWrapper);

        GroupVo groupVo = new GroupVo();
        groupVo.setGroupId(subjectGroup.getGroupId());
        int  count = getGroupNum(subjectGroup.getSubjectId(),subjectGroup.getCourseClassId());
        groupVo.setCount(String.valueOf(count));
        groupVo.setGroupLeaderName(getGroupLeaderName(subjectGroup.getGroupLeader()));
        groupVo.setSubjectName(getSubjectName(subjectGroup.getSubjectId()));
        groupVo.setSubjectId(subjectGroup.getSubjectId());
        groupVo.setCourseClassId(subjectGroup.getCourseClassId());

        return groupVo;

    }

    public Integer getGroupNum(String subjectid, String classid ){

        QueryWrapper<SelectSubject> queryWrapper = new QueryWrapper();
        queryWrapper.eq("subject_id",subjectid);
        queryWrapper.eq("course_class_id",classid);

        Map<String, Object> objectMap = selectSubjectService.getMap(queryWrapper);


        if (objectMap!=null){
            return objectMap.size()+1;
        }
        return  0;


    }
    public String getGroupLeaderName(String studentId){
        QueryWrapper<Student> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",studentId);

        Student student = studentService.getOne(queryWrapper);
        if(student!=null){
            return student.getStuName();
        }else{
            return "";
        }


    }
    public String getSubjectName(String subjectId){

        Subject subject = subjectService.getById(subjectId);

        if(subject!=null){
            return subject.getSubjectName();
        }else{
            return "";
        }


    }
}
